package com.auoem.radio.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.auctus.am.framework.constants.AmConstants;
import com.auctus.am.framework.entity.ChannelInf;
import com.auctus.am.framework.imp.RadioManager;
import com.auctus.am.framework.utils.LogM;
import com.auoem.radio.base.BaseFragment;
import com.auoem.radio.R;
import com.auoem.radio.common.adapter.BindingAdapter;
import com.auoem.radio.databinding.FragmentChannelBinding;

import java.util.List;
import java.util.function.IntConsumer;

public class ChannelFragment extends BaseFragment implements IntConsumer {

    private FragmentChannelBinding mBinding;

    private List<ChannelInf> channelInfList;

    private BindingAdapter<ChannelInf> channelAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        mBinding = FragmentChannelBinding.inflate(inflater);
        initData();
        return mBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateChannel();
    }

    private void initData() {
        channelInfList = RadioManager.getInstance().getRadioData().getChannelList();
        channelAdapter = new BindingAdapter<>(getActivity(), channelInfList, R.layout.adapter_item_ch);
        channelAdapter.setOnItemClickListener(this);
        mBinding.lvChannel.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.lvChannel.setAdapter(channelAdapter);
        LogM.d("ChFragment", "initData: " + channelInfList.size());
    }

    private void updateChannel() {
        channelAdapter.notifyDataSetChanged();
    }

    @Override
    public void accept(int value) {
//        selCh(value);//todo 应该切换信道后进入编辑
        goEditChannel(channelInfList.get(value));
    }

    @Override
    public IntConsumer andThen(IntConsumer after) {
        return null;
    }

    @Override
    public void onClick(View v) {

    }

    private void goEditChannel(ChannelInf channelInf) {
        Intent intent = new Intent(getActivity(), ChannelInfActivity.class);
        intent.putExtra(AmConstants.KEY_CHANNEL_ID, channelInf.channelId);
        Bundle bundle = new Bundle();
        bundle.putParcelable(AmConstants.KEY_CHANNEL_OBJ, channelInf);
        intent.putExtras(bundle);
        startActivity(intent);
    }

}
